package com.test6;

import java.io.File;

public class ListTraverseTest {
    public static void main(String[] args) {
//        listDir(new File("D:\\Pictures"));
        deleteDir(new File("C:\\Users\\LDH\\Desktop\\aaa"));
    }
    //递归遍历文件夹
    public static void listDir(File dir) {
        File[] files = dir.listFiles();
        System.out.println(dir.getAbsolutePath());
        if (files != null && files.length >0) {
            for (File file : files) {
                if (file.isDirectory()){
                    listDir(file); // 递归
                }
                System.out.println(file.getAbsolutePath());
            }
        }
    }
    //递归删除文件夹
    public static void deleteDir(File dir){
        File[] files = dir.listFiles();
        if (files != null && files.length>0){
            for (File file : files){
                if (file.isDirectory()){
                    deleteDir(file); //递归
                }
                System.out.println(file.getAbsolutePath()+"删除:"+file.delete());
            }
        }
        System.out.println(dir.getAbsolutePath()+"删除:"+dir.delete());
    }
}
